﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Production.ApplicationLib.Command;
using Production.ApplicationLib.Queries;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;

namespace Production.WebApi.Controllers
{
    [ApiController]
    public class ProductionOrderController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IProductionOrderQueries _productionOrderQueries;

        public ProductionOrderController(IMediator mediator,
            IProductionOrderQueries productionOrderQueries)
        {
            _mediator = mediator;
            _productionOrderQueries = productionOrderQueries;
        }

        [HttpGet(ApiRoutes.ProductionOrder.QueryProductionOrders)]
        public async Task<IActionResult> Get([FromQuery] ProductionOrderSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _productionOrderQueries.QueryProductionOrdersByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _productionOrderQueries.QueryProductionOrders(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.ProductionOrder.CreateProductionOrder)]
        public async Task<IActionResult> Post(CreateProductionOrderCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.ProductionOrder.UpdateProductionOrder)]
        public async Task<IActionResult> Put(int id, UpdateProductionOrderCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.ProductionOrder.GetProductionOrder)]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _productionOrderQueries.GetProductionOrder(id);
            return Ok(result);
        }

        [HttpPost(ApiRoutes.ProductionOrder.GenerateProductionOrder)]
        public async Task<IActionResult> Generate()
        {
            var result = await _mediator.Send(new GenerateProductionOrderCommand());
            return Ok(result);
        }
    }
}